package com.example.electronicmall.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.electronicmall.R;
import com.example.electronicmall.bean.CommdityData;
import com.example.electronicmall.bean.CommdityOneBean;
import com.example.electronicmall.bean.LeftModel;

import java.util.List;

/**
 * Created by
 * Time: 19:04
 * Author: Lenovo
 */
public class CommodityLeftAdapter extends RecyclerView.Adapter<CommodityLeftAdapter.leftHolder> {

    private Context context;
    private List<LeftModel> beanList;

    private int leftPosition=0;

    private onItemClickListener onItemClickListener=null;

    public CommodityLeftAdapter(Context context) {
        this.context = context;
    }

    public void setBeanList(List<LeftModel> beanList) {
        this.beanList = beanList;
    }

    public void setOnItemClickListener(CommodityLeftAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setLeftPosition(int leftPosition) {
        this.leftPosition = leftPosition;
    }

    @NonNull
    @Override
    public leftHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {

        return new leftHolder(LayoutInflater.from(context).inflate(R.layout.claddfiyleft_list_item,null));
    }

    @Override
    public void onBindViewHolder(@NonNull  CommodityLeftAdapter.leftHolder holder, int position) {
        holder.aname.setText(beanList.get(position).getLeftName());

        //如果左边的条目被选中
        if(leftPosition==position){
            holder.relativeLayout.setBackgroundColor(Color.WHITE);
            holder.aname.setTextColor(Color.parseColor("#65b0c0"));
        }else {
            holder.relativeLayout.setBackgroundColor(Color.parseColor("#edf4fc"));
            holder.aname.setTextColor(Color.parseColor("#313939"));
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener!=null){
                    onItemClickListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return null==beanList ?0 :beanList.size();
    }


    class leftHolder extends RecyclerView.ViewHolder{

        RelativeLayout relativeLayout;
        TextView aname;

        public leftHolder(@NonNull View itemView) {
            super(itemView);
            relativeLayout=itemView.findViewById(R.id.rl_left);
            aname=itemView.findViewById(R.id.left_tv);
        }
    }

    public interface onItemClickListener{
        void onItemClick(int position);
    }
}
